package org.dlnu.xeon.controller;

import lombok.extern.slf4j.Slf4j;

import org.dlnu.xeon.controller.Response.Response;
import org.dlnu.xeon.domain.User;
import org.dlnu.xeon.domain.Vaccination;
import org.dlnu.xeon.service.VaccinationService;
import org.dlnu.xeon.service.UserService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static org.dlnu.xeon.common.consts.StaticData.NUMBER_ZERO;
import static org.dlnu.xeon.common.enums.StatusCode.*;

@Slf4j
@RestController
@RequestMapping("/vaccination")
public class VaccinationController {
    @Resource
    private VaccinationService vaccinationService;

    @Resource
    private UserService userService;

    @PostMapping("/selectAllVac")
    public Response selectAllVac(){
        List<Vaccination> vaccinations = vaccinationService.selectAllVac();
        if (null == vaccinations) {
            log.error("[Boot2022|UserController|selectUser] the result is null！");
            return new Response(DATA_NOT_EXIST, null, "the result--user is null");
        }
        return new Response(SERVICE_RUN_SUCCESS, vaccinations, "Service running successfully");
    }

    @PostMapping("/getVacMsgByDid")
    public Response selectVacByDid(@RequestBody Vaccination vaccination) {
        if (null == vaccination.getDid() || NUMBER_ZERO > vaccination.getDid()) {
            log.error("[Boot2022|UserController|selectUser] the parameter uid is error. uid = " + vaccination.getDid());
            return new Response(PARAMETER_VALIDATE_ILLEGAL, vaccination.getDid(), "did parameter error");
        }
        Vaccination resvaccination = vaccinationService.selectVacByDid(vaccination.getDid());
        if (null == resvaccination) {
            log.error("[Boot2022|UserController|selectUser] the result is null！");
            return new Response(DATA_NOT_EXIST, null, "the result--user is null");
        }
        return new Response(SERVICE_RUN_SUCCESS, resvaccination, "Service running successfully");
    }

    @PostMapping("/getVacMsgByUid")
    public Response selectVacByUid(@RequestBody Vaccination vaccination) {
        if (null == vaccination.getUid() || NUMBER_ZERO > vaccination.getUid()) {
            log.error("[Boot2022|UserController|selectUser] the parameter uid is error. uid = " + vaccination.getUid());
            return new Response(PARAMETER_VALIDATE_ILLEGAL, vaccination.getUid(), "uid parameter error");
        }
        List<Vaccination> resvaccination = vaccinationService.selectVacByUid(vaccination.getUid());
        if (null == resvaccination) {
            log.error("[Boot2022|UserController|selectUser] the result is null！");
            return new Response(DATA_NOT_EXIST, null, "the result--user is null");
        }
        return new Response(SERVICE_RUN_SUCCESS, resvaccination, "Service running successfully");
    }

    @PostMapping("/getVacMsgBydose")
    public Response selectVacByDose(@RequestBody Vaccination vaccination){
        if (StringUtils.isEmpty(vaccination.getDose())) {
            log.error("[Boot2022|UserController|insertUser] the parameter username is error. username = " + vaccination.getDose());
            return new Response(PARAMETER_VALIDATE_ILLEGAL, vaccination.getDose(), "username parameter error");
        }
        List<Vaccination> vaccination1 = vaccinationService.selectVacByDose(vaccination.getDose());
        if (null == vaccination1) {
            log.error("[Boot2022|UserController|insertUser] the result is null！");
            return new Response(DATA_NOT_EXIST, null, "the result--uid is null");
        }
        return new Response(SERVICE_RUN_SUCCESS, vaccination1, "Service running successfully");
    }

    @PostMapping("/getVacMsgByName")
    public Response selectVacByName(@RequestBody User user){
        if (StringUtils.isEmpty(user.getName())) {
            log.error("[Boot2022|UserController|insertUser] the parameter username is error. username = " + user.getName());
            return new Response(PARAMETER_VALIDATE_ILLEGAL, user.getName(), "username parameter error");
        }
        User resuser = userService.selectUserByName(user.getName());
        if (null == resuser) {
            log.error("[Boot2022|UserController|selectUser] the result is null！");
            return new Response(DATA_NOT_EXIST, null, "the result--user is null");
        }
        List<Vaccination> vaccination = vaccinationService.selectVacByUid(resuser.getUid());
        if (null == vaccination) {
            log.error("[Boot2022|UserController|selectUser] the result is null！");
            return new Response(DATA_NOT_EXIST, null, "the result--user is null");
        }
        return new Response(SERVICE_RUN_SUCCESS, vaccination, "Service running successfully");
    }

    @PostMapping("/getVacMsgByIdNumber")
    public Response selectVacByIdNumber(@RequestBody User user){
        if (StringUtils.isEmpty(user.getId_number())) {
            log.error("[Boot2022|UserController|insertUser] the parameter username is error. username = " + user.getId_number());
            return new Response(PARAMETER_VALIDATE_ILLEGAL, user.getId_number(), "username parameter error");
        }
        User resuser = userService.selectUserByIdNumber(user.getId_number());
        if (null == resuser) {
            log.error("[Boot2022|UserController|selectUser] the result is null！");
            return new Response(DATA_NOT_EXIST, null, "the result--user is null");
        }
        List<Vaccination> vaccination = vaccinationService.selectVacByUid(resuser.getUid());
        if (null == vaccination) {
            log.error("[Boot2022|UserController|selectUser] the result is null！");
            return new Response(DATA_NOT_EXIST, null, "the result--user is null");
        }
        return new Response(SERVICE_RUN_SUCCESS, vaccination, "Service running successfully");
    }

    @PostMapping("/getVacMsgByNumber")
    public Response selectVacByNumber(@RequestBody User user){
        if (StringUtils.isEmpty(user.getNumber())) {
            log.error("[Boot2022|UserController|insertUser] the parameter username is error. username = " + user.getNumber());
            return new Response(PARAMETER_VALIDATE_ILLEGAL, user.getNumber(), "username parameter error");
        }
        User resuser = userService.selectUserByNumber(user.getNumber());
        if (null == resuser) {
            log.error("[Boot2022|UserController|selectUser] the result is null！");
            return new Response(DATA_NOT_EXIST, null, "the result--user is null");
        }
        List<Vaccination> vaccination = vaccinationService.selectVacByUid(resuser.getUid());
        if (null == vaccination) {
            log.error("[Boot2022|UserController|selectUser] the result is null！");
            return new Response(DATA_NOT_EXIST, null, "the result--user is null");
        }
        return new Response(SERVICE_RUN_SUCCESS, vaccination, "Service running successfully");
    }

    @PostMapping("/getVacMsgByUnit")
    public Response selectVacByUnit(@RequestBody User user){
        if (StringUtils.isEmpty(user.getUnit())) {
            log.error("[Boot2022|UserController|insertUser] the parameter username is error. username = " + user.getUnit());
            return new Response(PARAMETER_VALIDATE_ILLEGAL, user.getUnit(), "username parameter error");
        }
        User resuser = userService.selectUserByUnit(user.getUnit());
        if (null == resuser) {
            log.error("[Boot2022|UserController|selectUser] the result is null！");
            return new Response(DATA_NOT_EXIST, null, "the result--user is null");
        }
        List<Vaccination> vaccination = vaccinationService.selectVacByUid(resuser.getUid());
        if (null == vaccination) {
            log.error("[Boot2022|UserController|selectUser] the result is null！");
            return new Response(DATA_NOT_EXIST, null, "the result--user is null");
        }
        return new Response(SERVICE_RUN_SUCCESS, vaccination, "Service running successfully");
    }

    @PostMapping("/getVacMsgByBatchNumber")
    public Response selectVacByBatchNumber(@RequestBody Vaccination vaccination) {
        if (null == vaccination.getBatch_number()) {
            log.error("[Boot2022|UserController|selectUser] the parameter uid is error. uid = " + vaccination.getBatch_number());
            return new Response(PARAMETER_VALIDATE_ILLEGAL, vaccination.getBatch_number(), "uid parameter error");
        }
        Vaccination resvaccination = vaccinationService.selectVacByBatchNumber(vaccination.getBatch_number());
        if (null == resvaccination) {
            log.error("[Boot2022|UserController|selectUser] the result is null！");
            return new Response(DATA_NOT_EXIST, null, "the result--user is null");
        }
        return new Response(SERVICE_RUN_SUCCESS, resvaccination, "Service running successfully");
    }

    @PostMapping("/insertVac")
    public Response insertVac(@RequestBody Vaccination vaccination) {
        if (StringUtils.isEmpty(vaccination.getUid())) {
            log.error("[Boot2022|UserController|insertUser] the parameter username is error. username = " + vaccination.getUid());
            return new Response(PARAMETER_VALIDATE_ILLEGAL, vaccination.getUid(), "username parameter error");
        }
        Long did = vaccinationService.insertVac(vaccination);
        if (null == did) {
            log.error("[Boot2022|UserController|insertUser] the result is null！");
            return new Response(DATA_NOT_EXIST, null, "the result--uid is null");
        }
        return new Response(SERVICE_RUN_SUCCESS, did, "Service running successfully");
    }

    @PostMapping("/updateVac")
    public Response updateVac(@RequestBody Vaccination vaccination) {
        if (null == vaccination.getDid() || NUMBER_ZERO > vaccination.getDid()) {
            log.error("[Boot2022|UserController|updateUser] the parameter uid is error. uid = " + vaccination.getDid());
            return new Response(PARAMETER_VALIDATE_ILLEGAL, vaccination.getUid(), "uid parameter error");
        }
        Vaccination resvaccination = vaccinationService.updateVac(vaccination);
        if (null == resvaccination) {
            log.error("[Boot2022|UserController|updateUser] the result is null！");
            return new Response(DATA_NOT_EXIST, null, "the result--resUser is null");
        }
        return new Response(SERVICE_RUN_SUCCESS, resvaccination, "Service running successfully");
    }

    @PostMapping("/deleteVacByDid")
    public Response deleteVacByDid(@RequestBody Vaccination vaccination) {
        if (null == vaccination.getDid() || NUMBER_ZERO > vaccination.getDid()) {
            log.error("[Boot2022|UserController|deleteUser] the parameter uid is error. uid = " + vaccination.getDid());
            return new Response(PARAMETER_VALIDATE_ILLEGAL, vaccination.getDid(), "uid parameter error");
        }
        vaccinationService.deleteVacByDid(vaccination.getDid());
        return new Response(SERVICE_RUN_SUCCESS, null, "Service running successfully");
    }

    @PostMapping("/deleteVacByUid")
    public Response deleteVacByUid(@RequestBody Vaccination vaccination) {
        if (null == vaccination.getUid() || NUMBER_ZERO > vaccination.getUid()) {
            log.error("[Boot2022|UserController|deleteUser] the parameter uid is error. uid = " + vaccination.getUid());
            return new Response(PARAMETER_VALIDATE_ILLEGAL, vaccination.getUid(), "uid parameter error");
        }
        vaccinationService.deleteVacByUid(vaccination.getUid());
        return new Response(SERVICE_RUN_SUCCESS, null, "Service running successfully");
    }
}
